from model.subject import SubjectDao
from lib.validator import Validator
from lib.form import Form

from flask import request
from flask_login import login_user, current_user
import time


class SubjectService():
    def __init__(self):
        self.dao = SubjectDao()
        
    
    def get_subject_list(self):
        subjects = self.dao.all()
        return subjects

class SubjectValidator(Validator):
    def __init__(self):
        Validator.__init__(self)
              
        self.rules = {'body':{
                                'required' : [None, True],
                                'minlength':[None, 1],
                                'maxlength':[None, 256]
                               }
        }

    
class SubjectForm(Form):
    """ Submit user form
    """
    def __init__(self):
        """Only accept POST request
        """
        Form.__init__(self)
        self.validator = SubjectValidator()
        
        if self.is_submitted():
            inputs = self.get_raw_inputs()
            self.errors = self.validator.validate(inputs)
            if self.errors == []:
                self.body = self.request.values.get('body')
                
                
    def has_error(self):
        return self.errors == []